var util = require('../../../../../utils/util')   //引入配置公共请求接口
import { video } from '../../../../../config/config'
const app = getApp();

Page({
  data: {
    current: 0,
    videoData: [],
    imgUrl: app.globalData.imgUrl,
    videoUrl: getApp().globalData.videoUrl,
    statusHeight: app.globalData.StatusBar,
    goodsShow: false,
    detailData: {},
    defaultGoods: {},
    videoId: '',
    columnId: '',
    PlayTyle: false,
    PauseTyle: true,
    titlePicUrl: '',
    footprintGuid: '', //足迹全局id
    videoBtn: false,
    showStyle: '',
    searchProductList: [],
    sign: 0
  },
  onLoad: function (options) {
    app.getLogin((re)=>{
      if(!re){
        this.setData({
          authorizationPopup: true
        })
      }
    })
    this.getColumnBy(options.columnId, options.id)
    // this.getVideoData(57)
    this.addScanNum(options.id)
    this.setData({
      videoId: options.id,
      videoName: options.name,
      columnId: options.columnId
    })
  },
  goreBack: function (e) { //返回
    wx.navigateBack({
      delta: 1
    })
  },
  goHome: function (e) { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  appointmentRegBtn() {
    wx.navigateTo({
      url: '/packagBuy/pages/found/crmweb/index?v=2',
    })
  },
  goodsShowBtn() {
    this.setData({
      goodsShow: true
    })
  },
  goodsShowClose() {
    this.setData({
      goodsShow: false
    })
  },
  goodsClassify() {
    wx.navigateTo({
      url: '/pages/classify/classify'
    })
  },
  // 获取列表
  getColumnBy(id, optionVideoid){
    const userId = wx.getStorageSync('loginMsg').userId
    const pricePstCode =  wx.getStorageSync('addrCode').pricePstCode
    const url = video.getVideoUrl+'?pricePstCode='+pricePstCode+'&columnId='+id+'&userId='+userId
    app.reqFun.requestJson('POST', url, '', (res) => {
      if (res.data.success == 1) {
        let data = res.data.data
        for (var i = 0, l = data.length; i < l; i++) {
          data[i].PlayTyle = true
          data[i].PauseTyle = false
          data[i].showBtn = true
          if (optionVideoid == data[i].id) {
            if (data[i].searchProductList) {
              this.setData({
                searchProductList: data[i].searchProductList,
                sign: data[i].sign,
                defaultGoods: data[i].searchProductList[0],
              })
            }
            this.setData({
              current: i,
              showStyle: data[i].showStyle,
              isKeep:  data[i].isKeep
            })
          }
          // this.setData({
          //   // current: i,
          //   // showStyle: data[i].showStyle,
          //   isKeep:  data[i].isKeep
          // })
          if(data[i].titlePic) {
            if (data[i].titlePic.split('/')[0] === 'marketing') {
              data[i].titlePic = this.data.videoUrl + data[i].titlePic
            } else {
              data[i].titlePic = this.data.imgUrl + data[i].titlePic
            }
          }
          if(data[i].url) {
            if (data[i].url.split('/')[0] === 'marketing') {
              data[i].url = this.data.videoUrl + data[i].url
            } else {
              data[i].url = this.data.imgUrl + data[i].url
            }
          }
          if (data[i].backgroundPic) {
            data[i].backgroundPic = this.data.imgUrl + data[i].backgroundPic
          }
        }
        this.setData({
          videoData: data
        })
      }
      console.log('videoData', this.data.videoData)
    }, true)
  },
  // 获取视频数据
  // getVideoData(videoId) {
  //   const pricePstCode =  wx.getStorageSync('addrCode').pricePstCode
  //   const userId =  wx.getStorageSync('loginMsg').userId
  //   let url = ''
  //   url = video.getProductUrl+'?pricePstCode='+pricePstCode+'&videoId='+videoId+'&userId='+userId
  //   app.reqFun.requestJson('POST', url, '', (res) => {
  //     if (res.data.success == 1) {
  //       let data = res.data.data
  //       if (data.titlePic.split('/')[0] === 'marketing') {
  //         this.setData({
  //           titlePicUrl: getApp().globalData.videoUrl,
  //         })
  //       } else {
  //         this.setData({
  //           titlePicUrl: app.globalData.imgUrl,
  //         })
  //       }
  //       this.setData({
  //         detailData: data,
  //         isKeep: res.data.data.isKeep,
  //         videoId: videoId
  //       })
  //       if (data.searchProductList) {
  //         this.setData({
  //           defaultGoods: data.searchProductList[0],
  //         })
  //       }
  //     }
  //   }, true)
  // },
  // 增加浏览量
  addScanNum(videoId) {
    let url = video.addScanNumUrl+'?videoId='+videoId
    app.reqFun.requestJson('POST', url, '', (res) => {
      // if (res.data.success == 1) {
      // }
    }, true)
  },
  keepVideoBtn() {
    let isKeep = this.data.isKeep
    if (isKeep) {
      this.unKeepVideo()
    } else {
      this.keepVideo()
    }
  },
    // 收藏视频
    keepVideo(){
      const url = video.keepVideoUrl+'?videoId='+this.data.videoId+'&columnId='+this.data.columnId
      app.reqFun.requestJson('POST', url, '', (res) => {
        if (res.data.success == 1) {
          wx.showToast({
            title: '收藏成功',
          })
          let videoId = this.data.videoId
          // this.getVideoData(videoId)
          this.getColumnBy(this.data.columnId, videoId)
        }
      }, true)
    },
    // 取消收藏视频
    unKeepVideo(){
      const url = video.unKeepVideoUrl+'?videoId='+this.data.videoId
      app.reqFun.requestJson('POST', url, '', (res) => {
        if (res.data.success == 1) {
          let videoId = this.data.videoId
          // this.getVideoData(videoId)
          this.getColumnBy(this.data.columnId, videoId)
        }
      }, true)
    },
    goodsClassify() {
      wx.navigateTo({
        url: '/pages/classify/classify'
      })
    },
    goGoodsInfo(param) {
      const skuId = param.currentTarget.dataset.skuid
      wx.navigateTo({
        url: "/packageShop/pages/goodsInfo/goodsInfo?id=" + skuId
      })
    },
    goPlayVideo(e) { //开始播放视频
      var that = this;
      var videoplay = wx.createVideoContext('video')
      wx.getNetworkType({ // 获取网络类型
        success: function (res) {
          if (res.networkType != 'wifi') {
            that.setData({
              showNotWifiPaly: true
            })
            setTimeout(function () {
              that.setData({
                showNotWifiPaly: false
              })
            }, 1500)
          }
        }
      })
      videoplay.play()
      that.setData({
        swiperTyle: false,
        PlayTyle: true,
        PauseTyle: false
      })
    },
    goPauseVideo() {
      wx.createVideoContext('video').pause()
      this.setData({
        swiperTyle: true,
        PlayTyle: true,
        PauseTyle: false
      })
    },
    palyEndLitten() {
      this.setData({
        PlayTyle: false,
        PauseTyle: false
      })
    },
    clickVideo() {
      let that = this
      let PlayTyle = that.data.PlayTyle
      let PauseTyle = that.data.PauseTyle
      if (PlayTyle) {
        that.setData({
        PauseTyle: !PauseTyle
      })
      }
    },
  onPlay(e) {},

  onPause(e) {
     console.log('pause', e.detail.activeId)
  },
  onChange(e) {
    let that = this
    console.log('e.detail', e.detail)
    var item = e.detail.item
    console.log('item', item)
    var videoplay = wx.createVideoContext('video' + item.id, this)
    videoplay.play()
    let searchProductList = e.detail.item.searchProductList
    if (searchProductList) {
      that.setData({
        searchProductList: e.detail.item.searchProductList,
        sign: e.detail.item.sign,
        defaultGoods: e.detail.item.searchProductList[0],
      })
    } else {
      that.setData({
        searchProductList:[],
        defaultGoods: {},
      })
    }
    that.setData({
      isKeep: e.detail.item.isKeep,
      videoId: e.detail.item.id,
      sign: e.detail.item.sign,
      // showStyle: e.detail.item.showStyle,
    })
    console.log('this.data..searchProductList', that.data.searchProductList)
    // this.getVideoData(e.detail.activeId)
  },
    /**
   * 用户点击右上角分享
   */
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log('this.data.videoName', this.data.videoName)
    return {
      title: this.data.videoName,
      desc: '视频详情',
      path: `/packageLive/pages/video/detail/video-swiper/video-swiper?id=${this.data.videoId}&columnId=${this.data.columnId}&hid=${wx.getStorageSync('loginMsg').userId}`
      // path: `/packageLive/pages/video/detail/detail?id=${this.data.videoId}&hid=${wx.getStorageSync('loginMsg').userId}`
    }
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      footprintGuid: util.guid()
    })
    app.saveAccessFootprint(this.data.footprintGuid, 'videoInfo', this.data.videoId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.videoId)
  },
  onEnded(e) {},

  onError(e) {},

  onWaiting(e) {},

  onTimeUpdate(e) {},

  onProgress(e) {},

  onLoadedMetaData(e) {
    console.log('LoadedMetaData', e)
  }
})
